<?php
require_once 'buildCurl.php';
error_reporting(E_ALL || ~E_NOTICE);
header('content-type:application/json;charset=utf-8');
$res = json_decode(file_get_contents('php://input'), true);


$term0 = $res['term'][0];
$term1 = $res['term'][1];


$session = $res['session'];

$now_year = date('Y');

switch($term0){
    case 0: $term0 = ($now_year - 3)."-".($now_year - 2);break;
    case 1: $term0 = ($now_year - 2)."-".($now_year - 1);break;
    case 2: $term0 = ($now_year - 1)."-".($now_year - 0);break;
    case 3: $term0 = ($now_year - 0)."-".($now_year + 1);break;
}


switch($term1){
    case 0: $term1 = "-1-1";break;
    case 1: $term1 = "-2-1";break;
}


$url = "http://211.82.48.36/student/courseSelect/calendarSemesterCurriculum/index";

$myCurl = new MyCurl($url, $session);
$result = $myCurl->getResult();
$httpCode = $myCurl->getHttpCode();

if ($httpCode != 200) {
    $arr = array('err' => 1, 'msg' => '1 step');
    exit(json_encode($arr));
}


$result = explode('thisSemesterCurriculum',$result)[2];
$result = explode('ajaxStudentSchedule',$result)[0];


$url = "http://211.82.48.36/student/courseSelect/thisSemesterCurriculum".$result."ajaxStudentSchedule/past/callback";
$post = "planCode=".$term0.$term1;

$myCurl = new MyPostCurl($url, $session, $post, 0);
$result = $myCurl->getResult();
$httpCode = $myCurl->getHttpCode();

if($httpCode != 200){
    $arr = array('err'=>1, 'url' => $url);
    exit(json_encode($arr)); 
}



$content = json_decode($result, true);

$day1 =[];
$day2 =[];
$day3 =[];
$day4 =[];
$day5 =[];
$day6 =[];
$day7 =[];

$list0 = $content['dateList'][0]['selectCourseList'];
foreach($list0 as $value0){
    $teacher = $value0['attendClassTeacher'];
    $list = $value0['timeAndPlaceList'];
    if(is_array($list)){
        foreach($list as $value){
            $name =$value['coureName'];
            $start_c = $value['classSessions'];
            $length = $value['continuingSession'];
            $weeks = $value['weekDescription'];
            $weeks = explode("周",$weeks)[0];
            $calssDay = $value['classDay'];
            if(strpos($weeks,"-") > 0){
                $start_w = (int)explode("-",$weeks)[0];
                $end_w = (int)explode("-",$weeks)[1];
            }else{
                $start_w = (int)explode("第",$weeks)[1];
                $end_w = $start_w;
            }
            //$where = $value['campusName']." ".$value['teachingBuildingName'].$value['classroomName'];
            $where = $value['teachingBuildingName'].$value['classroomName'];
            $arr = array('name'=>$name,'teacher'=>$teacher,'start_c' =>$start_c,'length'=>$length,'start_w'=>$start_w,'end_w'=>$end_w,'where'=>$where);

            switch($calssDay){
            case 1:$day1[] = $arr;break;
            case 2:$day2[] = $arr;break;
            case 3:$day3[] = $arr;break;
            case 4:$day4[] = $arr;break;
            case 5:$day5[] = $arr;break;
            case 6:$day6[] = $arr;break;
            case 7:$day7[] = $arr;break;
            }
        }
    }
}




$temp_kb = array($day1,$day2,$day3,$day4,$day5,$day6,$day7);
$arr = array('err'=>0,'kebiao_data'=>$temp_kb);
exit(json_encode($arr));
